package com.moxiao.yfjh.module.blind.controller.admin.memberReport;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.alibaba.fastjson.JSON;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.*;
import com.moxiao.yfjh.module.blind.convert.memberReport.MemberReportConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberReport.MemberReportDO;
import com.moxiao.yfjh.module.blind.service.memberReport.MemberReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲交友举报")
@RestController
@RequestMapping("/blind/member-report")
@Validated
public class MemberReportController {

    @Resource
    private MemberReportService memberReportService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲交友举报")
    @PreAuthorize("@ss.hasPermission('blind:member-report:create')")
    public CommonResult<Long> createMemberReport(@Valid @RequestBody MemberReportCreateReqVO createReqVO) {
        return success(memberReportService.createMemberReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲交友举报")
    @PreAuthorize("@ss.hasPermission('blind:member-report:update')")
    public CommonResult<Boolean> updateMemberReport(@Valid @RequestBody MemberReportUpdateReqVO updateReqVO) {
        memberReportService.updateMemberReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲交友举报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:member-report:delete')")
    public CommonResult<Boolean> deleteMemberReport(@RequestParam("id") Long id) {
        memberReportService.deleteMemberReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲交友举报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:member-report:query')")
    public CommonResult<MemberReportRespVO> getMemberReport(@RequestParam("id") Long id) {
        MemberReportDO memberReport = memberReportService.getMemberReport(id);
        MemberReportRespVO memberReportRespVO = MemberReportConvert.INSTANCE.convert(memberReport);
        if (StringUtils.isNotEmpty(memberReportRespVO.getEvidenceImg())){
            memberReportRespVO.setEvidenceImgList(JSON.parseArray(memberReportRespVO.getEvidenceImg(), EvidenceUrl.class));
        }
        if (StringUtils.isNotEmpty(memberReportRespVO.getEvidenceVideo())){
            memberReportRespVO.setEvidenceVideoList(JSON.parseArray(memberReportRespVO.getEvidenceVideo(),EvidenceUrl.class));
        }
        return success(memberReportRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲交友举报列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:member-report:query')")
    public CommonResult<List<MemberReportRespVO>> getMemberReportList(@RequestParam("ids") Collection<Long> ids) {
        List<MemberReportDO> list = memberReportService.getMemberReportList(ids);
        return success(MemberReportConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲交友举报分页")
    @PreAuthorize("@ss.hasPermission('blind:member-report:query')")
    public CommonResult<PageResult<MemberReportRespVO>> getMemberReportPage(@Valid MemberReportPageReqVO pageVO) {
        PageResult<MemberReportDO> pageResult = memberReportService.getMemberReportPage(pageVO);
        PageResult<MemberReportRespVO> voPageResult = MemberReportConvert.INSTANCE.convertPage(pageResult);
        return success(voPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲交友举报 Excel")
    @PreAuthorize("@ss.hasPermission('blind:member-report:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberReportExcel(@Valid MemberReportExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberReportDO> list = memberReportService.getMemberReportList(exportReqVO);
        // 导出 Excel
        List<MemberReportExcelVO> datas = MemberReportConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲交友举报.xls", "数据", MemberReportExcelVO.class, datas);
    }

}
